﻿Shader "Dan/UI/UI_RotationUI"
{
    Properties
    {
        [Header(ColorInfo)]
        _ColorInside ("ColorInside", Color) = (0.07843138,0.3921569,0.7843137,1)
        _ColorOutside ("ColorOutside", Color) = (0.3921569,0.7843137,0.7843137,1)
        _ColorRamp("ColorRamp", Range(0, 1)) = 0.5
        _Emission ("Emission", Float ) = 1
        [Header(Texture)]
        _MainTex ("MainTex", 2D) = "white" {}
        [Header(Transform)]
        _Scale ("Scale", Range(0, 1)) = 1
        [Header(Rotation Speed)]
        _Speed_R ("Speed_R", Float ) = 1
        _Speed_G ("Speed_G", Float ) = -1
        _Speed_B ("Speed_B", Float ) = 1
        _Speed_A ("Speed_A", Float ) = 2
        _IdleTime ("Idle Speed", Float ) = 1
        
        [Header(Loop Rotation)]

        [MaterialToggle] _useLoop_R ("useLoop_R", Float ) = 0
        [MaterialToggle] _useLoop_G ("useLoop_G", Float ) = 0
        [MaterialToggle] _useLoop_B ("useLoop_B", Float ) = 0
        [MaterialToggle] _UseFadeOut ("UseFadeOutwithFlick", Float ) = 0
        

    }
    
    SubShader
    {

        Tags { "IgnoreProjector"="True"
            "Queue"="Transparent"
            "RenderType"="Transparent" }
        LOD 100    
        Blend SrcAlpha OneMinusSrcAlpha
        ZWrite Off 
 
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"
            #include "../ShaderLibs/UIFX.cginc"
            #include "../ShaderLibs/UVOperator.cginc"
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };
            
            struct v2f
            {
                float4 vertex : SV_POSITION;    
                float2 uv : TEXCOORD0;
                
            };

            float4 _ColorInside;
            float4 _ColorOutside;
            float _ColorRamp;
            sampler2D _MainTex; 
            float4 _MainTex_ST;
            float _Scale;
            float _Speed_R;
            float _Speed_G;
            float _Speed_B;
            float _Speed_A;
            float _Emission;
            float _IdleTime;
            fixed _useLoop_G;
            fixed _useLoop_B;
            fixed _useLoop_R;
            fixed _UseFadeOut;
            fixed _LoopSpeed;    

            v2f vert ( appdata v )
            {
                
                v2f o;
                UNITY_INITIALIZE_OUTPUT(v2f ,o);
                o.vertex = UnityObjectToClipPos(v.vertex);
                
                o.uv = v.uv;        
                return o;
            }
            
            
            fixed4 frag (v2f i ) : SV_Target
            {
                
                float2 uv = i.uv;
                uv =TRANSFORM_TEX(uv, _MainTex);
                
                 
                float2 scaleUV = saturate((uv*2-1 +_Scale)/ (_Scale*2) );
                
                
                float loopTime = _Time.y*0.25;//循环转动速度
                float sinTime = sin(_Time.y*_IdleTime);//待机状态速度
                float idleTime01 = (clamp(sinTime,-0.5,0)+clamp(sinTime,0.25,0.75))*0.35;
                float idleTime02 = (clamp(sinTime,-0.3,0.3)+clamp(sinTime,0.7,1)+clamp(sinTime,-1,-0.8))*0.25;
                
                //r通道的旋转
                float useLoopR = lerp( idleTime01, loopTime , _useLoop_R );//判断开启循环
                float radTime01 = _PI2*useLoopR*_Speed_R;//圆周速度
                float2 rotateUV_R = Rotate2D(scaleUV,0.5,radTime01);//旋转
                float channelR = tex2D(_MainTex,rotateUV_R).r;//采样
                

                
                //g通道的旋转
                float useLoopG = lerp( idleTime02 ,loopTime, _useLoop_G );
                float radTime02 = _PI2*useLoopG*_Speed_G;
                float2 rotateUV_G = Rotate2D(scaleUV,0.5,radTime02);
                float channelG = tex2D(_MainTex,rotateUV_G).g;
                
                //b通道的旋转
                float useLoopB = lerp( idleTime02 ,loopTime, _useLoop_B );
                float radTime03 = _PI2*useLoopB*_Speed_B;
                float2 rotateUV_B = Rotate2D(scaleUV,0.5,radTime03);
                float channelB = tex2D(_MainTex,rotateUV_B).b;
                
                
                //a通道的旋转
                float radTime04 = _Speed_A*_Time.y; 
                float2 rotateUV_A = Rotate2D(scaleUV,0.5,radTime04);
                float channelA = tex2D(_MainTex,rotateUV_A).a;
                //闪烁淡出
                float2 rd = float2(_Time.y,_Time.y)*0.001;
                float flick = Flick(rd);
                float fadeout = saturate((step(0.5,flick)+(_Scale*2-1)));
                fadeout =lerp(1,fadeout,_UseFadeOut);
                
                //色彩渐变
                float colorRamp = saturate(length(scaleUV*1.5-0.75)/_ColorRamp);
                float3 finalColor = lerp(_ColorInside.rgb,_ColorOutside.rgb,colorRamp)*_Emission;


                return fixed4(finalColor,saturate(channelR+channelG+channelB+channelA)*fadeout);
            
                

                


                
                
            }
            ENDCG
        }
    }
    
    
    
}
